import Command.RemoteController;
import Factory.*;
import Observer.EnvironmentObserver;
import Observer.EnvironmentSensorNetwork;
import Observer.SmartMachineObserver;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MachineManager extends SystemManager {
    private MachineFactory machineFactory;
    private EnvironmentSensorNetwork sensorNetwork;
    private RemoteController remoteController;
    private List<AgriculturalMachine> machines = new ArrayList<>();

    public MachineManager(Scanner scanner, EnvironmentSensorNetwork sensorNetwork, RemoteController remoteController) {
        super(scanner);
        this.machineFactory = new SmartMachineFactory();
        this.sensorNetwork = sensorNetwork;
        this.remoteController = remoteController;
    }

    @Override
    public void manage() {
        while (true) {
            System.out.println("\n=== 农机设备管理 ===");
            System.out.println("1. 创建农机设备");
            System.out.println("2. 查看农机状态");
            System.out.println("3. 返回主菜单");

            int choice = getUserChoice(1, 3, "请输入选择(1-3): ");

            if(choice == 3){
                return;
            }

            switch (choice) {
                case 1:
                    createMachine();
                    break;
                case 2:
                    checkMachineStatus();
                    break;
                case 3:
                    return;
            }
        }
    }

    private void createMachine() {
        System.out.println("\n选择要创建的农机类型:");
        System.out.println("1. 播种机");
        System.out.println("2. 喷洒机");
        System.out.println("3. 收割机");
        System.out.println("4. 拖拉机");
        //System.out.println("5. 除草机");
        System.out.println("返回");

        int choice = getUserChoice(1, 6, "请输入选择(1-5): ");
        if (choice == 6) return;

        MachineType type = MachineType.values()[choice - 1];

        // 修复：使用工厂创建农机并正确注入传感器网络
        AgriculturalMachine machine = machineFactory.createMachine(type, sensorNetwork);

        if (machine == null) {
            System.out.println("创建失败：不支持的农机类型");
            return;
        }

        // 保存创建的农机
        machines.add(machine);
        remoteController.addMachine(machine);


        // 注册为环境观察者
        if (machine instanceof EnvironmentObserver) {
            sensorNetwork.registerObserver((EnvironmentObserver) machine);
            System.out.println("已创建 " + type + " 并注册为环境观察者");
        } else {
            System.out.println("已创建 " + type + "，但该类型不支持环境观察");
        }
    }

    // 添加选择农机的方法
    private AgriculturalMachine selectMachine() {
        if (machines.isEmpty()) {
            System.out.println("尚未创建任何农机");
            return null;
        }
        System.out.println("\n选择要检查的农机:");
        for (int i = 0; i < machines.size(); i++) {
            System.out.printf("%d. %s (状态: %s)\n",
                    i + 1,
                    machines.get(i).getType(),
                    machines.get(i).getState());
        }
        int choice = getUserChoice(1, machines.size(), "请选择农机(1-" + machines.size() + "): ");
        return machines.get(choice - 1);
    }

    // 修改状态检查方法调用方式
    private void checkMachineStatus() {
        AgriculturalMachine machine = selectMachine();
        if (machine == null) {
            System.out.println("未检测到农机设备");
            return;
        }

        System.out.println("===== 农机状态报告 =====");
        remoteController.displayMachineStatus(machine);
        System.out.println("=====================");
    }

    // 添加 getMachines() 方法
    public List<AgriculturalMachine> getMachines() {
        return machines;
    }
}


